<?php

class frontPromo {

var $html;
var $promo;
	
	// A mortal sin of ours is that we don't comment... At all.
	function getFrontPromo() {
	
		$html = "";
		$randompromo = array();
		
		$html .= "<h2>Featured Promotions</h2>";
		
		// This rand() apparently gets slow on large datasets, so if we start getting up to like a few hundered thousand promotions we might want to change it...
		$result = "SELECT * FROM `promotions` ORDER BY rand() LIMIT 6";
			
			// I'm gonna smeggin smack this thing in a minute, such an annoyance...
			$db = new Connection();
			$sql = mysql_query($result) or die (mysql_error());
			
			// Finally got this returning a full 6 random results!
			while($row = mysql_fetch_array( $sql )) {
			
				$image = $row['promotionalImages'];
				$split = explode(";", $image);
				if ($split) {
					$html .= "<a href=\"selectimage.php?sec=ps&category=".$row['category']."&promotion=".$row['id']."\"><img src=\"images/promoimages/".$split[0]."\" alt=\"".$split[0]."\" /></a>\n";
				} else if ($image == "") {
				
				} else {
					$html .= "<a href=\"selectimage.php?sec=ps&category=".$row['category']."&promotion=".$row['id']."\"><img src=\"images/promoimages/".$row['promotionalImages']."\" alt=\"".$row['promotionalImages']."\" /></a>\n";
				}
				
			}
		
		echo $html;
			
	}

	function getFrontCountPromo() {
		
		$html = "";
		$randompromo = array();
		
		$html .= "<h2>Featured Promotions</h2>";
		
		$result = "SELECT * FROM `promotions` ORDER BY rand() LIMIT 6";
			
			// I'm gonna smeggin smack this thing in a minute, such an annoyance...
			$db = new Connection();
			$sql = mysql_query($result) or die (mysql_error());
			
			// Turns out that actually works, although heck knows why, how does mysql understand PHP rand()?
			while($row = mysql_fetch_array( $sql )) {
			
				$image = $row['promotionalImages'];
				$split = explode(";", $image);
				if ($split) {
					$html .= "<a href=\"selectimage.php?sec=ps&category=".$row['category']."&promotion=".$row['id']."\"><img src=\"images/promoimages/".$split[0]."\" alt=\"".$split[0]."\" /></a>\n";
				} else if ($image == "") {
				
				} else {
					$html .= "<a href=\"selectimage.php?sec=ps&category=".$row['category']."&promotion=".$row['id']."\"><img src=\"images/promoimages/".$row['promotionalImages']."\" alt=\"".$row['promotionalImages']."\" /></a>\n";
				}
				
			}
		
		echo $html;
			
	}
			
				
	function getFrontCalendar($sector, $cssNumber) {
		$db = new Connection();
			
			$html = "";
			$html .= "<table class=\"calendarTable".$cssNumber."\">\n";
			$html .= "<tr id=\"firstRow\">\n";
			$html .= "<td class=\"header\">January</td>\n";
			$html .= "<td class=\"header\">February</td>\n";
			$html .= "<td class=\"header\">March</td>\n";
			$html .= "<td class=\"header\">April</td>\n";
			$html .= "<td class=\"header\">May</td>\n";
			$html .= "<td class=\"header lastCol\">June</td>\n";
			$html .= "</tr>\n";

			$html .= "<tr id=\"firstRowPromos\">\n";
			$datearr = array(1, 2, 3, 4, 5, 6);
				$html .= $this->getCalendarRow($datearr, $sector);
			$html .= "</tr>"; 
			$html .= "<tr id=\"secondRow\">\n";
			$html .= "<td class=\"header\">July</td>\n";
			$html .= "<td class=\"header\">August</td>\n";
			$html .= "<td class=\"header\">September</td>\n";
			$html .= "<td class=\"header\">October</td>\n";
			$html .= "<td class=\"header\">November</td>\n";
			$html .= "<td class=\"header lastCol\">December</td>\n";
			$html .= "</tr>\n";
			$html .= "<tr id=\"secondRowPromos\">\n";
			$datearr = array(7, 8, 9, 10, 11, 12);
				$html .= $this->getCalendarRow($datearr, $sector);
			$html .= "</tr>";
			$html .= "</table>";
				switch ($sector) {
					case 1: // sector is just deli
					$themePromoCat = 3;
					break;
					default:
					$themePromoCat = 3;
					break;
				}
		//	$html .= $this->buildThemePromoTable($sector, $themePromoCat, $cssNumber);
				return $html;
			}
			
			function getCalendarRow($datearr, $sector){
				$db = new Connection();
				foreach ($datearr as $i => $value) {
					$html .= "<td class=\"foodMonthlyPromos\">";
					$result = $db->selectQuery("SELECT * FROM `promotions` WHERE MONTH(targetdate) = ".$value." AND `subcategory` = ".$sector." ORDER BY `category` ASC, `targetdate` ASC;");
					//Display Retrieved information
						while($row = mysql_fetch_array( $result )) {
							if ($row['frontDisplay'] == 1) {
								$html .= "<p class=\"nameOfPromotion bold cat".$row['category']."\"><a href=\"selectimage.php?sec=ps&amp;category=".$row['category']."&amp;promotion=".$row['id']."\">".$row['nameOfPromotion']."</a><br/><span class=\"subhead\">".$this->formatDate($row['targetdate'])."</span></p>\n";
							}else{
								$html .= "<p class=\"nameOfPromotion cat".$row['category']."\">".$row['nameOfPromotion']."<br/><span class=\"subhead\">".$this->formatDate($row['targetdate'])."</span></p>\n";
							}
						}
					$html .= "</td>";
				}
				return $html;
			}
			
			function buildThemePromoTable($sector, $themePromoCat, $cssNumber){
			
				$db = new Connection();
				$result = $db->selectQuery("SELECT * FROM `promotions` WHERE `sector` = ".$sector." AND `category` = ".$themePromoCat."  ORDER BY `category` ASC, `targetdate` ASC;");
				$amountOfRows = mysql_num_rows($result);
				$modremainder = $amountOfRows % 6;
				
				$remainder = 6 - $modremainder;
				
				
				
				$html .= "<table class=\"calendarTable".$cssNumber."\">\n";
				$html .= "<tr>\n";
				$html .= "<td  colspan=\"6\" class=\"header\">Theme Promos</td>\n";
				$html .= "<tr>\n<tr>\n";
				$count = 0;
				while($row = mysql_fetch_array( $result )) {
				if($count == 6)
  					  {
     				   $count = 0;
      				  $html .= "</tr><tr>";
   					 }
   				 $count++;

					if ($row['frontDisplay'] == 1) {
						$html .= "<td class=\"nameOfPromotion bold cat".$row['category']."\"><a href=\"selectimage.php?sec=ps&amp;category=".$row['category']."&amp;promotion=".$row['id']."\">".$row['nameOfPromotion']."</a><br/><span class=\"subhead\">".$this->formatDate($row['targetdate'])."</span></td>\n";
					}else{
						$html .= "<td class=\"nameOfPromotion bold cat".$row['category']."\">".$row['nameOfPromotion']."<br/><span class=\"subhead\">".$this->formatDate($row['targetdate'])."</span></td>\n";
					}
				}
				
				// add the remainder of <td>s
				if ($remainder > 0){
					for ($i = 0; $i < $remainder; $i++){
						$html .= "<td class=\"nameOfPromotion bold\"></td>";
					}
				
				}
				
				
				$html .= "</tr>\n";
				$html .= "</table>";
				
				return $html;
			
			}
			function formatDate($date){
				$arr = explode("-", $date);
				return ($arr[2]."-".$arr[1]."-".$arr[0]);
			}

	// It's worth noting that promotions will be passed in as an array, if they're not, you're a bad person. We could also put the promos into a database table somewhere, though thats more than likely overkill.
	function featuredPromos($promo) {
	
		// Check this connection boi!
		$db = new Connection();
	
		// Yo whats up it's your boy MrHammerFisch!
		$html = "";
		
		// We 'bout to trip this shit up with some featured promos
		foreach ($promo as $p) {
		
			// Ooh shoot nigga, we gonna bust up this array and we gonna get 2 promotions based on their name, chiiiiiiit
			$sql = "SELECT * FROM `promotions` WHERE `nameOfPromotion` = '".$p."' LIMIT 1";
			
			// Aww hell yuh, query string up in this muh-fugga.
			$result = mysql_query($sql);
			
				// Thinking up relevant variable names becoming more difficult, urge to write $foo or $awesome is strong... must... resist...
				while ($feature = mysql_fetch_array($result)) {
				
					// Wahoo I did it, now we gotta get up all in the display o' this bitch
					$html .= "<a href=\"selectimage.php?sec=ps&category=".$feature['category']."&promotion=".$feature['id']."\" style=\"margin-right: 25px;\">";
						
						$images = $feature['promotionalImages'];
						// Time to start using explode properly, split is being deprecated in PHP 6
						$split = explode(";", $images);
							if (count($split) > 1) {
								$html .= "<img src=\"images/promoimages/".$split[0]."\" alt=\"".$split[0]."\" />";
							} else {
								$html .= "<img src=\"images/promoimages/".$images."\" alt=\"".$images."\" />";
							}
							
					$html .= "</a>";
				
				}
		}
		
		echo $html;
	
	}
	
}

?>